import 'dart:convert';

import 'package:amap_flutter_search/search.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
    AmapFlutterSearch.initKey(
        iosAppKey: "39baf3898166c6f0c3704ade788fde01",
        androidAppKey: "c1121ac00f6d69b22bbd9ff3a04eccc4",
        initBack: (code, message) {
          print("initKey: $code, $message");
        });
    AmapFlutterSearch.updatePrivacyShow(hasShow:true,hasContains: true);
    AmapFlutterSearch.updatePrivacyAgree(hasAgree: true);
  }

  void _search(){
    //关键词查询 返回大列表
    AmapFlutterSearch.keywordsSearch( keyWord:"天安门", cityCode:"", page : 1, limit : 10, back:(code,data){});
    //输入提示 返回最近几条数据，cityLimit 是否限制在当前城市
    AmapFlutterSearch.inputTips( newText:"天安门", city:"北京", cityLimit : false, back:(code,data){});
    //通过Id查询详情，通过关键词查询/输入提示会返回POI-id
    AmapFlutterSearch.getPOIById( id:"h0000", back:(code,data){});
    //线路规划 wayPoints:点集合，支持最大18个点（起点+终点+16个途径点），drivingMode：偏好
    AmapFlutterSearch.routeSearch(wayPoints:[LatLng(26.638016, 106.795478),LatLng(26.556585, 106.768857)],drivingMode:DrivingMode.DRIVING_SINGLE_DEFAULT, back:(code,data){});
    //货车线路规划 wayPoints:点集合，支持最大18个点（起点+终点+16个途径点），drivingMode：偏好，truckInfo：车辆信息
    AmapFlutterSearch.truckRouteSearch(
      wayPoints:[LatLng(26.638016, 106.795478),LatLng(26.556585, 106.768857)],
      drivingMode:DrivingMode.DRIVING_SINGLE_DEFAULT,
      truckInfo:TruckInfo(plateProvince: "京",plateNumber: "A85A84",truckAxis: 4,truckHeight: 2.5,truckLoad: 4.2),
      back:(code,data){},
    );
    //地理编码
    AmapFlutterSearch.geocoding(address: "贵阳双龙数据工厂",back:(code,data){});
    //逆地理编码
    AmapFlutterSearch.reGeocoding(point: LatLng(26.556585, 106.768857),back:(code,data){});
  }

  String text = "点击功能返回结果";
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('请在地图plugin测试'),
        ),
        body: SingleChildScrollView(child: Column(
          children: [
            ElevatedButton( child: Text("关键词查询:天安门"),onPressed: (){
              AmapFlutterSearch.keywordsSearch( keyWord:"天安门", cityCode:"", page : 1, limit : 10, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("周边查询:LatLng(26.556585, 106.768857)"),onPressed: (){
              AmapFlutterSearch.boundSearch(page : 1, limit : 10, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              }, point: LatLng(26.556585, 106.768857));
            }),
            ElevatedButton( child: Text("POI详情:BV10006499"),onPressed: (){
              AmapFlutterSearch.getPOIById( id:"BV10006499", back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("输入提示:天安"),onPressed: (){
              AmapFlutterSearch.inputTips( newText:"天安", city:"北京", cityLimit : false, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("线路规划:[LatLng(45.807283, 126.568021),LatLng(22.793247, 100.947589)]"),onPressed: (){
              AmapFlutterSearch.routeSearch(wayPoints:[LatLng(45.807283, 126.568021),LatLng(22.793247, 100.947589)],drivingMode:DrivingMode.DRIVING_SINGLE_DEFAULT, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("货车线路规划:[LatLng(45.807283, 126.568021),LatLng(22.793247, 100.947589)]"),onPressed: (){
              AmapFlutterSearch.truckRouteSearch(truckInfo:TruckInfo(
                  plateProvince:"贵",
                  plateNumber:"AB5158",
                  truckAxis:2.0,
                  truckHeight: 2.4,
                  truckWidth:2.6,
                  truckLoad: 3.2,
                  truckWeight: 4.6
              ),wayPoints:[LatLng(45.807283, 126.568021),LatLng(22.793247, 100.947589)],drivingMode:DrivingMode.DRIVING_SINGLE_DEFAULT, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("线路规划(多点):[LatLng(26.638016, 106.795478),LatLng(26.630416, 106.765305),LatLng(26.556585, 106.768857)]"),onPressed: (){
              AmapFlutterSearch.routeSearch(wayPoints:[LatLng(26.638016, 106.795478),LatLng(26.630416, 106.765305),LatLng(26.556585, 106.768857)],drivingMode:DrivingMode.DRIVING_SINGLE_DEFAULT, back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("地理编码:贵阳双龙数据工厂"),onPressed: (){
              AmapFlutterSearch.geocoding(address: "贵阳双龙数据工厂",back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            ElevatedButton( child: Text("逆地理编码：LatLng(26.556585, 106.768857)"),onPressed: (){
              AmapFlutterSearch.reGeocoding(point: LatLng(26.556585, 106.768857),back:(code,data){
                setState(() {
                  text = json.encode(data);
                  debugPrint(text);
                });
              });
            }),
            Card(
              child: Text(text),
            )
          ]))),
    );
  }
}
